#ifndef _TIMER_8BIT_ASYNC_H_
#define _TIMER_8BIT_ASYNC_H_

#include <avr/io.h>
#include <avr/interrupt.h>
#include "../../../../bit/bit.h"
#include "../../../Component.h"

namespace Hardware
{
namespace Avr
{
	typedef struct TIMER_8BIT_ASYNC_CONFIG
	{
		unsigned char force : 1;
		unsigned char waveform : 2;
		unsigned char compareMode : 2;
		unsigned char clockSelect : 3;
		unsigned char count : 8;
		unsigned char compare : 8;
		unsigned char async : 1;
		unsigned char compareInterrupt : 1;
		unsigned char overflowInterrupt : 1;
		
	} Timer8BitAsyncConfig;

	typedef struct TIMER_8BIT_ASYNC_DATA
	{
	} Timer8BitAsyncData;

	class Timer8BitAsync : public Component<Timer8BitAsyncConfig,
						Timer8BitAsyncData>
	{
		public :

		Timer8BitAsync(){}
		Timer8BitAsync(Timer8BitAsyncConfig c){configuration=c;}

		void config()
		{
			//set force 
			TCCR2 = SetBit(TCCR2, FOC2, configuration.force);
			//set waveform
			TCCR2 = SetBit(TCCR2, WGM20,
				 GetBit(configuration.waveform, 0));
			TCCR2 = SetBit(TCCR2, WGM21,
				 GetBit(configuration.waveform, 1));
			//set compare match mode.
			TCCR2 = SetBitGroup(TCCR2, 2, COM20,
				configuration.compareMode);
			//set clock select prescaler
			TCCR2 = SetBitGroup(TCCR2, 3, CS20,
				configuration.clockSelect);
			//set count
			TCNT2 = configuration.count;
			//set compare
			OCR2 = configuration.compare;
			//set async
			ASSR = SetBit(ASSR, AS2, configuration.async);
			//set compare interrupt
			TIMSK = SetBit(TIMSK, OCIE2, 
				configuration.compareInterrupt);
			//set overflow interrupt
			TIMSK = SetBit(TIMSK, TOIE2, 
				configuration.overflowInterrupt);	
		}

	};
}}





#endif
